package game;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class PictureFrame extends JFrame {
    //定义一个二维数字，用来存储图片的编号
        int[][] datas = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,0}
        };
        //判断移动是否成功
    public boolean isSuccess(){
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if(datas[i][j] != winDatas[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
        
        //定义移动成功后的数组
    private int[][] winDatas ={
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,0}
        };

        //定义两个int类型的变量，用于记录0号图片的位置
    private int x0;
    private int y0;

    //定义按钮
    private JButton topButton;
    private JButton leftButton;
    private JButton bottomButton;
    private JButton rightButton;
    private JButton helpButton;
    private JButton resetButton;

    private JPanel imagePanel;

    public PictureFrame(){
        //窗体的基本设置
        initFrame();

        //二维数组的打乱
        randomData();

        //窗体上组件的绘制
        paintView();

        //给按钮设置点击事件
        addButtonEvent();

        //设置窗体可见
        this.setVisible(true);

    }
    //移动成功的操作
    public void success(){
        datas = new int[][]{
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
        };
        //按钮无法点击
//        topButton.setEnabled(false);
//        bottomButton.setEnabled(false);
//        leftButton.setEnabled(false);
//        rightButton.setEnabled(false);
    }

    //移动的图形重新绘制
    public void rePaintView(){
        //移除面板上所有组件
        imagePanel.removeAll();

        //遍历二位数字，得到图片编号
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j <datas[i].length ; j++) {
                //创建JLabel对象，加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("\\data\\JavaDemo\\images\\"+datas[i][j]+".png"));
                imageLabel.setBounds(j*90,i*90,90,90);
                imagePanel.add(imageLabel);
            }
        }

        //把面板添加到窗体上
        this.add(imagePanel);

        //重新绘制窗体
        imagePanel.repaint();
    }
    //给按钮设置点击事件
    public void addButtonEvent(){
        topButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("上");
                //边界的处理
                if(x0==3){
                    return;
                }
                //位置交换
                datas[x0][y0]=datas[x0+1][y0];
                datas[x0+1][y0]=0;
                x0=x0+1;

                //判断移动是否成功
                  if (isSuccess()){
                      success();
                  }

                //调用重绘方法
                rePaintView();
            }
        });
        leftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("左");
                //边界的处理
                if(y0 == 3){
                    return;
                }

                //位置交换
                datas[x0][y0]=datas[x0][y0+1];
                datas[x0][y0+1]=0;
                y0=y0+1;

                //判断移动是否成功
                if (isSuccess()){
                    success();
                }

                //调用重绘方法
                rePaintView();
            }
        });
        bottomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("下");
                //边界的处理
                if(x0 == 0){
                    return;
                }

                //位置交换
                datas[x0][y0]=datas[x0-1][y0];
                datas[x0-1][y0]=0;
                x0=x0-1;

                //判断移动是否成功
                if (isSuccess()){
                    success();
                }

                //调用重绘方法
                rePaintView();
            }
        });
        rightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("右");
                //边界的处理
                if(y0 == 0){
                    return;
                }

                //位置交换
                datas[x0][y0]=datas[x0][y0-1];
                datas[x0][y0-1]=0;
                y0=y0-1;

                //判断移动是否成功
                if (isSuccess()){
                    success();
                }

                //调用重绘方法
                rePaintView();
            }
        });
        helpButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("求助");
                success();
                rePaintView();
            }
        });
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("重置");
                datas = new int[][]{
                        {1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12},
                        {13,14,15,0}
                };
                randomData();
                rePaintView();
                topButton.setEnabled(true);
                leftButton.setEnabled(true);
                bottomButton.setEnabled(true);
                rightButton.setEnabled(true);
            }
        });
    }

    //二维数组的打乱
    public  void  randomData(){
        Random random = new Random();
        for (int i = 0; i < datas.length ; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x = random.nextInt(datas.length);
                int y = random.nextInt(datas[i].length);

                int temp = datas[i][j];
                datas[i][j] = datas[x][y];
                datas[x][y] = temp;
            }
        }
        //用于记录0号图片的位置
        wc:for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                    if (datas[i][j] == 0){
                        x0 = i;
                        y0 = j;
                        break wc;
                    }
            }
        }
        System.out.println(x0+" "+y0);
    }

    //窗体上组件的绘制
    public void paintView(){
        //标题图片
        JLabel titleLabel = new JLabel(new ImageIcon("\\data\\JavaDemo\\images\\title.png"));
        titleLabel.setBounds(354,27,232,57);
        this.add(titleLabel);

//        //定义一个二维数字，用来存储图片的编号
//        int[][] datas = {
//                {1,2,3,4},
//                {5,6,7,8},
//                {9,10,11,12},
//                {13,14,15,16}
//        };
        //创建面板
        imagePanel = new JPanel();
        imagePanel.setBounds(150,114,360,360);
        imagePanel.setLayout(null);
        //遍历二位数字，得到图片编号
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j <datas[i].length ; j++) {
                //创建JLabel对象，加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("\\data\\JavaDemo\\images\\"+datas[i][j]+".png"));
                imageLabel.setBounds(j*90,i*90,90,90);
                imagePanel.add(imageLabel);
            }
        }
        //把面板添加到窗体上
        this.add(imagePanel);

        //动漫参照图
        JLabel canZhaoTuLabel = new JLabel(new ImageIcon("\\data\\JavaDemo\\images\\canzhaotu.png"));
        canZhaoTuLabel.setBounds(574,114,122,121);
        this.add(canZhaoTuLabel);

        //上、下、左、右，求助、重置按钮
         topButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\shang.png"));
        topButton.setBounds(732,265,57,57);
        this.add(topButton);

         leftButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\zuo.png"));
        leftButton.setBounds(650,347,57,57);
        this.add(leftButton);

         bottomButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\xia.png"));
        bottomButton.setBounds(732,347,57,57);
        this.add(bottomButton);

         rightButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\you.png"));
        rightButton.setBounds(813,347,57,57);
        this.add(rightButton);

         helpButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\qiuzhu.png"));
        helpButton.setBounds(626,444,108,45);
        this.add(helpButton);

         resetButton = new JButton(new ImageIcon("\\data\\JavaDemo\\images\\chongzhi.png"));
        resetButton.setBounds(786,444,108,45);
        this.add(resetButton);

        //显示背景图
        JLabel backgroundImage = new JLabel(new ImageIcon("\\data\\JavaDemo\\images\\background.png"));
        backgroundImage.setBounds(0,0,960,530);
        this.add(backgroundImage);
    }

    public void initFrame(){
        //窗体大小
        this.setSize(960,565);
        //窗体标题
        this.setTitle("动漫拼图");
        //窗体居中
        this.setLocationRelativeTo(null);
        //窗体关闭时退出应用程序
        this.setDefaultCloseOperation(3);
        //窗体位于其他窗体默认布局
        this.setAlwaysOnTop(true);
        //取消窗体默认布局
        this.setLayout(null);
    }
}
